package de.webtech.faceplace.pages;

import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.Request;
import org.hibernate.Session;

import de.webtech.faceplace.entities.Messages;
import de.webtech.faceplace.entities.Person;

public class PrivateMessages {
	
	@Inject
	private Request request;
	
	@Inject
	private Session session;
	
	
	public Person getPerson() {
		Long id = (long) 0;
		try {
			id = Long.parseLong(request.getParameter("uid"));
		} catch (Exception e) {
		}
		Person p = null;
		if (id != 0) {
			for (Object pp : session.createCriteria(Person.class).list()) {
				Person ppp = (Person) pp;
				if (ppp.id == id) {
					return ppp;
				}
			}

		}
		return (Person) request.getSession(true).getAttribute("user");

	}
	
	public String getFirstName() {
		return getPerson().firstName;
	}

	public String getLastName() {
		return getPerson().lastName;
	}
	
	@Property
	private String privateMessage;
	
	public void onSubmitFromPrivateMessageForm(){
		Person p = getPerson();
		Messages m = new Messages(privateMessage, p, 0);
		p.pushNewPrivateMessage(m);
		
		session.update(p);
		
	}
	
}
